package com.ocom.oauth.utils;

import com.anji.captcha.util.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

@Component
public class UrlUtil {
    private static String serverPort;
    @Value("${server.port}")
    private String port;

    @PostConstruct
    public void setServerPort(){
        serverPort= port;
    }

    /**
     * 获取当前服务的ip和端口
     * @return
     */
    public static String getUrl() {
        InetAddress address = null;
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        assert address != null;
        return "http://"+address.getHostAddress() +":"+UrlUtil.serverPort;
    }


    public static final String getRemoteId(HttpServletRequest request) {
        String ip = request.getHeader("remoteip");
        String ua = request.getHeader("user-agent");
        if (StringUtils.isNotBlank(ip)) {
            return ip + ua;
        }
        return request.getRemoteAddr() + ua;
    }

}
